@extends('admin.common')

@section('warp_head')
    <style>
        .input-size { width: 80px;}
    </style>
    <h1>
        {{ $info['name'] }}
        <small>列表</small>
    </h1>

@endsection

@section('warp_content')
    <div class="box">
        <div class="box-header">
            <h3 class="box-title"></h3>
            <div class="pull-left">
                <a href="/admin/taskconfig" class="btn btn-default">返回列表</a>
            </div>
        </div>
        <!-- /.box-header -->
        <div class="box-body table-responsive no-padding">
            <table class="table table-hover text-center" id="mytable">
                <input type="hidden" id="config_type" value="{{ $info['type'] }}">
                <tr>
                    <th></th>
                    @if(!empty($info['config']))
                        @foreach($info['config']['th'] as $k=>$v)
                        <th class="val-th">{{ $v }}</th>
                        @endforeach
                    @endif
                    <th>操作</th>
                </tr>
                <tr>
                    <td>奖励</td>
                    @if(!empty($info['config']))
                        @foreach($info['config']['td'] as $k=>$v)
                            <td class="val-td"><input type="text" class="text-center input-size" value="{{ $v }}"></td>
                        @endforeach
                    @endif
                    <td>
                        <a href="javascript:void(0)" id="btn-add">新增</a>
                        <a href="javascript:void(0)" id="btn-save">保存</a>
                    </td>
                </tr>
            </table>
        </div>
    </div>
@endsection

@section('script')
    <script>
        $(function () {
            //增加操作
            $('#btn-add').click(function(){
                var th = $('#mytable').find('tr>th');
                var th_len = th.length;
                var config_type = $('#config_type').val();
                var no = parseInt(th_len)>2 ? parseInt(th_len)-1 : 1;
                var th_html = '<th class="val-th">第'+no+config_type+'</th>';
                var td = $('#mytable').find('tr>td');
                //var td_len = td.length;
                var td_html = '<td class="val-td"><input type="text" class="text-center input-size" value=""></td>';
                $(th).eq(th_len-1).before(th_html);
                $(td).eq(th_len-1).before(td_html);
            });

            //保存操作
            $('#btn-save').click(function(){
                var th = [];
                var td = [];
                $('.val-th').each(function (index , item) {
                    th.push($(item).text());
                });
                $('.val-td > input').each(function (index , item) {
                    td.push($(item).val());
                });
//                console.log(th, td);
                $.ajax({
                    url: '/admin/taskconfig/opreatSave',
                    data: {
                        task_id : "{{ request('task_id') }}",
                        th: th,
                        td: td
                    },
                    type: 'post',
                    dataType : 'json',
                    headers: {
                        'X-CSRF-TOKEN': "{{ csrf_token() }}"
                    },
                    success: function(ret){
                        if(ret.code == 1) {
                            swal({
                                title: "保存成功",
                                type: "success",
                                timer: 1000,
                                showConfirmButton: false
                            });
                        }else{

                        }
                    }
                });
                return false;
            });
        });
    </script>
@endsection